package com.czzn.czkongzhi;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
    private Thread th;
    private static final int UPDATE_TEXT=1;
    //private Beuse beuse;
    //定义一个SurfaceHolder进行绘制
    private SurfaceHolder sfh;
    //定义一个画布
    private Canvas canvas;
    //定义一个画笔
    private Paint paint;
    private boolean flag;
    //初始化监听接口
    private OnDownActionListener mDown=null;
    private OnUpActionListener mUp=null;
    /*
     *固定摇杆背景圆形以及摇杆圆形的X,Y坐标以及半径
     */
    //获取整个屏幕大小
    public WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    //设置背景圆的X点
    private int RockerCircleX = wm.getDefaultDisplay().getWidth()/2;
    //设置背景圆的Y点
    private int RockerCircleY = wm.getDefaultDisplay().getHeight()/2-140;
    //设置背景圆的半径
    private int RockerCircleR = 350;
    //设置摇杆的X点
    public float SmallRockerCircleX = wm.getDefaultDisplay().getWidth()/2;
    //设置摇杆的Y点
    public float SmallRockerCircleY = wm.getDefaultDisplay().getHeight()/2-140;
    //设置摇杆的半径
    private float SmallRockerCircleR = 100;
    public short dx;
    public short dy;
    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //保存屏幕常亮
        this.setKeepScreenOn(true);
        //获得SurfaceHolder对象
        sfh = this.getHolder();
        //添加回调函数
        sfh.addCallback(this);
        //新建画笔
        paint = new Paint();
        //抗锯齿
        paint.setAntiAlias(true);
        //设置焦点
        setFocusable(true);
        //获取焦点
        setFocusableInTouchMode(true);
    }
    public void surfaceCreated(SurfaceHolder holder) {
        th = new Thread(this);
        flag = true;
        th.start();
    }
    /***
     * 得到两点之间的弧度
     */
    public double getRad(float px1, float py1, float px2, float py2) {
        //得到两点X的距离
        float x = px2 - px1;
        //得到两点Y的距离
        float y = py1 - py2;
        //算出斜边长
        float xie = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        //得到这个角度的余弦值（通过三角函数中的定理 ：邻边/斜边=角度余弦值）
        float cosAngle = x / xie;
        //通过反余弦定理获取到其角度的弧度
        float rad = (float) Math.acos(cosAngle);
        //注意：当触屏的位置Y坐标<摇杆的Y坐标我们要取反值-0~-180
        if (py2 < py1) {
            rad = -rad;
        }
        return rad;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE) {
            // 当触屏区域不在活动范围内
            if (Math.sqrt(Math.pow((RockerCircleX - (int) event.getX()), 2)
                    + Math.pow((RockerCircleY - (int) event.getY()), 2)) >= RockerCircleR) {
                //得到摇杆与触屏点所形成的角度
                double tempRad = getRad(RockerCircleX, RockerCircleY, event.getX(), event.getY());
                //保证内部小圆运动的长度限制
                getXY(RockerCircleX, RockerCircleY, RockerCircleR, tempRad);
            } else {//如果小球中心点小于活动区域则随着用户触屏点移动即可
                SmallRockerCircleX = (int) event.getX();
                SmallRockerCircleY = (int) event.getY();
                Log.e("TAG","X="+SmallRockerCircleX+",Y="+SmallRockerCircleY);
                if (mDown != null) {
                    mDown.onDown((int)SmallRockerCircleX,(int)SmallRockerCircleY);
                }
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            //当释放按键时摇杆要恢复摇杆的位置为初始位置
            SmallRockerCircleX = wm.getDefaultDisplay().getWidth()/2;
            SmallRockerCircleY = wm.getDefaultDisplay().getHeight()/2-140;
            Log.e("TAG","X圆点="+SmallRockerCircleX+",Y圆点="+SmallRockerCircleY);
            if (mUp != null) {
                mUp.OnUp((int)SmallRockerCircleX, (int)SmallRockerCircleY);
            }
        };
        return true;
    }
    /**
     *
     * @param R
     *            圆周运动的旋转点
     * @param centerX
     *            旋转点X
     * @param centerY
     *            旋转点Y
     * @param rad
     *            旋转的弧度
     */
    public void getXY(float centerX, float centerY, float R, double rad) {
        //获取圆周运动的X坐标
        SmallRockerCircleX = (float) (R * Math.cos(rad)) + centerX;
        //获取圆周运动的Y坐标
        SmallRockerCircleY = (float) (R * Math.sin(rad)) + centerY;
    }
    public void draw() {
        try {
            canvas = sfh.lockCanvas();
            canvas.drawColor(Color.WHITE);
            //设置透明度
            paint.setColor(0x70000000);
            //绘制摇杆背景
            canvas.drawCircle(RockerCircleX, RockerCircleY, RockerCircleR, paint);
            paint.setColor(0x70ff0000);
            //绘制摇杆
            canvas.drawCircle(SmallRockerCircleX, SmallRockerCircleY,SmallRockerCircleR,paint);
            if(!RockerActivity.isGra) {
                dx = (short) (SmallRockerCircleX - wm.getDefaultDisplay().getWidth() / 2);
                dy = (short) (SmallRockerCircleY - wm.getDefaultDisplay().getHeight() / 2);
            }
            short left = (short)(-1*(dx-dy));
            short right = (short)(dx+dy);
            if(left>350){left=350;}else if(left<-350){left=-350;}
            if(right>350){right=350;}else if(right<-350){right=-350;}


            //Log.v("Offset",left+":"+right);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                if (canvas != null)
                    sfh.unlockCanvasAndPost(canvas);
            } catch (Exception e2) {
            }
        }
    }
    public void run() {
        // TODO Auto-generated method stub
        while (flag) {
            draw();
            try {
                Thread.sleep(50);
            } catch (Exception ex) {
            }
        }
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.v("TAG", "surfaceChanged");
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
        Log.v("TAG", "surfaceDestroyed");
    }


    public void setOnDownActionListener(OnDownActionListener down) {
        mDown = down;
    }

    public void setOnUpActionListener(OnUpActionListener up) {
        mUp = up;
    }

    //定义两个个接口
    public interface OnDownActionListener {
        public void onDown(int x, int y);
    }

    public interface OnUpActionListener {
        public void OnUp(int x, int y);
    }
}
